


var ajax = require("../ajax/ajax");
var mongo = require("../mongo/mongo");

var validator = require("validator");
var moment = require("moment");
var py = require("../comm/py");
var mongoose = require('mongoose');
const task = require('../dict/task');
const yz = require('../comm/yz');
const array = require('../comm/array');
const fFlow = require('../flow/fFlow');
const rong = require('../comm/rong');

exports.exeNode = async function (customerOrderId, flowClassId, nodeId) {
    // body...
    customerOrderId = customerOrderId.toString()
    return new Promise(async (resolve, reject) => {

        var err = "";
        var b = {};

        var customerOrder = await mongo.findOneAsync({
            data: {
                _id: customerOrderId
            },
            db: "uzor",
            collection: "customerOrder"
        })

        var node = null;
        //开始执行



        var nodeList = customerOrder.flow[flowClassId].nodeList;
        nodeList.forEach((_node) => {


            if (nodeId == _node.id) {
                node = _node
                return false;
            }

        }, this);
        if (node == null) {



            return {
                err: "没有id为" + nodeId + "的node节点"
            }
        }

        node.statusId = yz.IsNullThen(node.statusId, 0)
        if (node.statusId >= 200) {
            return {
                err: "节点执行完毕"
            }

        }





        var updateFind = { _id: mongoose.Types.ObjectId(customerOrderId) };

        updateFind["flow." + flowClassId + ".nodeList.id"] = nodeId



        var updateSet = { $set: {} }; //修改此节点状态为100 

        updateSet.$set["flow." + flowClassId + ".nodeList.$.statusId"] = 100


        //    if (node.taskClassId == "root" || node.taskClassId == "default") { //如果是根节点或者没有任务的默认节点的话,修改为200
        if (1 == 2) {
            updateSet.$set["flow." + flowClassId + ".nodeList.$.statusId"] = 200
            updateSet.$set["flow." + flowClassId + ".nodeList.$.jd"] = 0 //任务进度


            nodeList.forEach((element) => {
                if (element.rely) {

                    element.rely.forEach(async (relyNodeId) => {


                        if (node.id == relyNodeId) {
                            //如果是起始节点被执行了. 这是依赖起始节点的节点, 也需要被执行
                            await this.exeNode(customerOrderId, flowClassId, element.id);


                        }
                    }, this);
                }


            }, this);

        }
        else {

            if (node.statusId < 100) {

                //此节点还没有被执行

                var taskObj = {};
                taskObj.name = "来自订单[" + customerOrder.name + "]的" + task.getClassName(node.taskClassId) + "任务";
                taskObj.taskClassId = node.taskClassId;
                taskObj.managers = node.managers
                taskObj.members = node.members
                taskObj.companyCode = customerOrder.companyCode
                taskObj.nodeId = node.id
                taskObj.statusId = 100   //未接受任务
                taskObj.customerOrderId = customerOrderId  //关联的订单id
                taskObj.flowClassId = flowClassId
                taskObj = task.fmtTask(taskObj);



                //写入一条任务数据
                taskObj.namePy = py.firstLetter(taskObj.name);
                var saveRe = await mongo.SaveAsync({
                    data: taskObj,
                    db: "uzor",
                    collection: "task"

                });

                var itemArray = array.hebing(taskObj.managers, taskObj.members)

                var toUserIdArray = [];
                itemArray.forEach(function (item) {
                    toUserIdArray.push(item._id)
                }, this);
                await rong.sendMsg({
                    toUserId: toUserIdArray,
                    content: taskObj.name,
                    extra: {
                        event: "countMe"
                    }
                }, { noLog: true })
            }
            else if (node.statusId >= 200) {

                resolve({
                    err: "节点执行完毕"
                })

            }
            updateSet.$set["flow." + flowClassId + ".nodeList.$.taskId"] = taskObj._id.toString()
        }



        var reUpdate = await mongo.updateAsync(updateFind, updateSet, "customerOrder", "uzor");
        resolve({

            err: null,
            data: "ok"
        })

    });

};